/**
 * Created by LC on 2019/11/12
 */
package com.hw.paltfrom.service.doncutor;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.hw.paltfrom.mapper.HwBaseAnnexesfileMapper;
import com.hw.paltfrom.mapper.HwBaseUserMapper;
import com.hw.paltfrom.mapper.HwBaseWorkflowstepMapper;
import com.hw.paltfrom.mapper.HwCommandWorkflowMapper;
import com.hw.paltfrom.pojo.HwBaseAnnexesfile;
import com.hw.paltfrom.pojo.HwBaseUser;
import com.hw.paltfrom.pojo.HwBaseWorkflowstep;
import com.hw.paltfrom.pojo.HwCommandWorkflow;
import com.hw.paltfrom.rescode.HWSystemCode;
import com.hw.paltfrom.utils.ResponeResultUtil;
import com.hw.paltfrom.vo.TimeScrollVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class CommonWorkflowService {

    @Autowired
    private HwBaseWorkflowstepMapper baseWorkflowstepMapper;

    @Autowired
    private HwCommandWorkflowMapper commandWorkflowMapper;

    @Autowired
    private HwBaseAnnexesfileMapper annexesfileMapper;

    @Autowired
    private HwBaseUserMapper userMapper;

    //初始化共同页面
    public Map<String,Object> findPage(String workflowCode,String workflowType){
        Wrapper<HwBaseWorkflowstep> wrapper = new EntityWrapper<>();
        wrapper.eq("WorkFlow_Code",workflowCode).eq("WorkFlow_Type",workflowType);
        List<HwBaseWorkflowstep> hwBaseWorkflowsteps = baseWorkflowstepMapper.selectList(wrapper);
        if (hwBaseWorkflowsteps.size() <= 0){
            return ResponeResultUtil.fail(HWSystemCode.PARAMS_ERROR);
        }
        return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS,hwBaseWorkflowsteps);
    }

    //时间轴
    public Map<String,Object> findTimeScroll(String taskId){
        //根据ObjectId查询,对应指挥调度ID、执法监管ID、设备管理ID
        Wrapper<HwCommandWorkflow> wrapper = new EntityWrapper<>();
        wrapper.eq("OBJECT_ID",taskId).orderBy("CommandWorkFlow_DataTime",false);
        List<HwCommandWorkflow> hwCommandWorkflows = commandWorkflowMapper.selectList(wrapper);
        if (hwCommandWorkflows.size() == 0){
            return ResponeResultUtil.fail(HWSystemCode.DATA_NOTFOUND);
        }
        //将复检表FolderId转换为文件path,放入annexfile字段,path以","隔开
        List<TimeScrollVO> timeScrollVOList = new LinkedList<>();
        for (HwCommandWorkflow hwCommandWorkflow : hwCommandWorkflows) {
            TimeScrollVO timeScrollVO = new TimeScrollVO();
            timeScrollVO.setWorkflowModule(hwCommandWorkflow.getWorkflowModule());
            timeScrollVO.setWorkflowType(hwCommandWorkflow.getWorkflowType());
            timeScrollVO.setWorkflowCode(hwCommandWorkflow.getWorkflowCode());
            timeScrollVO.setCommandworkflowButtonname(hwCommandWorkflow.getCommandworkflowButtonname());
            //
            String fAccount = hwCommandWorkflow.getfAccount();
            Wrapper<HwBaseUser> userWrapper = new EntityWrapper<>();
            userWrapper.eq("F_Account",fAccount);
            List<HwBaseUser> hwBaseUsers = userMapper.selectList(userWrapper);
            if (hwBaseUsers.size() == 1){
                HwBaseUser hwBaseUser = hwBaseUsers.get(0);
                timeScrollVO.setfAccount(hwBaseUser.getfRealname());
            }
            timeScrollVO.setfFromdepcode(hwCommandWorkflow.getfFromdepcode());
            timeScrollVO.setfFromdepname(hwCommandWorkflow.getfFromdepname());
            timeScrollVO.setCommandworkflowDatatime(hwCommandWorkflow.getCommandworkflowDatatime());
            timeScrollVO.setCommandworkflowContent(hwCommandWorkflow.getCommandworkflowContent());
            timeScrollVO.setObjectId(hwCommandWorkflow.getObjectId());
            try{
                if (StringUtils.isNotBlank(hwCommandWorkflow.getfAnnexes())){
                    String[] guids = hwCommandWorkflow.getfAnnexes().split(",");
//            String paths = "";
                    List<List> fileLists = new ArrayList<>();
                    for (String guid : guids) {
                        Wrapper<HwBaseAnnexesfile> fileWrapper = new EntityWrapper<>();
                        fileWrapper.eq("F_FolderId", guid).orderBy("F_CreateDate");
                        List<HwBaseAnnexesfile> hwBaseAnnexesfiles = annexesfileMapper.selectList(fileWrapper);
                        fileLists.add(hwBaseAnnexesfiles);
                    }
                    timeScrollVO.setBaseAnnexesfileList(fileLists);
                }else {
                    timeScrollVO.setBaseAnnexesfileList(new ArrayList<>());
                }
            }catch (Exception e){
                e.printStackTrace();
            }
           timeScrollVOList.add(timeScrollVO);
        }
        return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS,timeScrollVOList);
    }

}
